#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped)
    {
        vector<int>ret;
        int j = 0, i = 0, n = pushed.size();
        while (i < n)
        {
            ret.push_back(pushed[i]);
            while (!ret.empty() && ret.back() == popped[j])
            {
                ret.pop_back();
                j++;
            }
            i++;
        }
        return ret.empty();
    }
};